import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "../store"
import { Notification } from 'element-ui';

Vue.use(VueRouter)

const routes = [
  {
    path: '/Home',
    component: () => import('@/components/Home/ShopHome.vue'),
    children: [
      //首页
      {
        path: '/shopIndex',
        naem: "shopIndex",
        component: () => import("@/components/Home/shopIndex.vue")
      },
      //全部商品
      {
        path: '/shopAll',
        naem: "shopAll",
        component: () => import("@/components/ShopAll/shopAll.vue"),
        children: [
          {
            path: 'goods',
            name: "shop",
            component: () => import("@/components/ShopAll/goodsDetail.vue"),
          },
          {
            path: '/error',
            name: "Error",
            component: () => import("@/components/Error/MyError.vue"),
          },

        ],

        beforeEnter: (to, from, next) => {
          // ...
          console.log(to);
          if (JSON.stringify(store.state.userLoginMsg) == "{}") {
            store.state.loginDialog = true
            Notification.success({ title: "登录", error: "请先登录" })
            if (store.state.loginDialog == false) {
              next()
            }
          } else {
            // next(false)
            next()
          }

        }
      },
      //关于我们
      {
        path: '/AboutMy',
        naem: "AboutMy",
        component: () => import("@/components/AboutMy/aboutMy.vue")
      },
      //商品详情页

      {
        path: '/details',
        name: "details",
        component: () => import("@/components/Details/details.vue")
      },
      // 购物车页面
      {
        path: "/shoppingCart",
        name: "shoppingCart",
        component: () => import("@/components/Shopping/shopping.vue"),
        beforeEnter: (to, from, next) => {
          // ...
          console.log(to);
          if (JSON.stringify(store.state.userLoginMsg) == "{}") {
            store.state.loginDialog = true
            Notification.success({ title: "登录", error: "请先登录" })
            if (store.state.loginDialog == false) {
              next()
            }
          } else {
            // next(false)
            next()
          }

        }
      },
      // 订单页面
      {
        path: "/confirmOrder",
        name: "confirmOrder",
        component: () => import("@/components/ConfirmOrder/confirmOrder.vue"),
      },
      {
        path: "/order",
        name: "order",
        component: () => import("@/components/Order/order.vue"),
        beforeEnter: (to, from, next) => {
          // ...
          console.log(to);
          if (JSON.stringify(store.state.userLoginMsg) == "{}") {
            store.state.loginDialog = true
            Notification.success({ title: "登录", error: "请先登录" })
            if (store.state.loginDialog == false) {
              next()
            }
          } else {
            // next(false)
            next()
          }

        }
      },
      {
        path: "/collect",
        name: "collect",
        component: () => import("@/components/Collect/collect.vue"),
        beforeEnter: (to, from, next) => {
          // ...
          console.log(to);
          if (JSON.stringify(store.state.userLoginMsg) == "{}") {
            store.state.loginDialog = true
            Notification.error({ title: "登录", message: "请先登录" })
            if (store.state.loginDialog == false) {
              next()
            }
          } else {
            // next(false)
            next()
          }

        }
      },

      // 重定向
      {
        path: "/",
        redirect: "/shopIndex"
      }
    ]
  },
  {
    path: "/",
    redirect: "/Home"
  }

]
//控制台会报[NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated"}]。其原因在于Vue-router在3.1之后把$router.push()方法改为了Promise。所以假如没有回调函数，错误信息就会交给全局的路由错误处理，因此就会报上述的错误。
//禁止全局路由错误处理打印，这个也是vue-router开发者给出的解决方案：
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
};

const router = new VueRouter({
  routes
})

export default router
